{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "twenty-address", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\nIt is also possible to add multiline comments using the triple quotes notation.\\nTechnically, this is a string and not a comment, but it can effectively be used\\nfor long, multiline comments.\\n'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Lines in a Python script (*.py file) that begin using the number sign are \n", "# comments. It is good practice to thoroughly comment your scripts.\n", "# The small initial investment in time to comment your work will save you\n", "# large amounts of time in the long run." ] }, { "cell_type": "code", "execution_count": 3, "id": "organized-magic", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Once deleted, variables cannot be recovered. Proceed (y/[n])? y\n" ] } ], "source": [ "#A useful command to run in the IPython console before running a new script\n", "#is '%reset'. This command will clear all previous variable assignments.\n", "%reset" ] }, { "cell_type": "code", "execution_count": 5, "id": "accurate-ethics", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Addition, subtraction, multiplication, and division\n", "3 + 5" ] }, { "cell_type": "code", "execution_count": 6, "id": "colonial-warner", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "# Alternatively, you can assign the result of the operation to a variable and \n", "# then print that variable.\n", "x = 3 + 5\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 8, "id": "toxic-syracuse", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.25\n" ] } ], "source": [ "# The synatax for the other common operations are as you'd expect:\n", "y = (3 + 5*2)/4\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 9, "id": "extensive-injury", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "y = 3.25\n" ] } ], "source": [ "# You could format your outputs too:\n", "print('y =', y)" ] }, { "cell_type": "code", "execution_count": 10, "id": "loaded-madrid", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "# To do powers, use the ** notation.\n", "print (3**2)\n", "\n", "# Note that '^' is not used to evaluate powers in Python. It is the XOR\n", "# (exclusive OR) logic operator." ] }, { "cell_type": "code", "execution_count": 12, "id": "medieval-orlando", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hbar = 1.05e-34 Js & kB = 1.38e-23 J/K\n" ] } ], "source": [ "# Python will accept scientific notation as follows:\n", "hbar = 1.05e-34\n", "kB = 1.38e-23\n", "print('hbar =', hbar, 'Js', '& kB =', kB, 'J/K')" ] }, { "cell_type": "code", "execution_count": 13, "id": "quality-industry", "metadata": {}, "outputs": [], "source": [ "# Some of the other common mathematical functions, like trig functions,\n", "# exponentials, and logs require a module. For example, cos(1) won't\n", "# work without the math module. I recommend the numPy module. To load the math module, use:\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 14, "id": "several-cancellation", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.5403023058681398\n" ] } ], "source": [ "# We can now evaluate the cosine function using\n", "x = np.cos(1)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 15, "id": "independent-crawford", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pi = 3.141592653589793 & e = 2.718281828459045\n" ] } ], "source": [ "# Pi and e are also available in the numPy module.\n", "print('pi =', np.pi, '& e =', np.e)" ] }, { "cell_type": "code", "execution_count": 17, "id": "rental-secretariat", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.4999681658280706\n" ] } ], "source": [ "# Here's an inverse trig function\n", "print(np.arctan(9999)/np.pi)" ] }, { "cell_type": "code", "execution_count": 18, "id": "bizarre-mambo", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.36787944117144233\n", "-1.0\n", "-2.0\n" ] } ], "source": [ "# Here are the exponential and logarithmic functions\n", "print(np.exp(-1))\n", "print(np.log(np.exp(-1))) # natural log, i.e. ln\n", "print(np.log10(10**-2)) # log (base 10)" ] }, { "cell_type": "code", "execution_count": 19, "id": "chemical-obligation", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.280109889280518\n", "7.280109889280518\n" ] } ], "source": [ "# There are two ways to calculate square roots\n", "print(53**0.5)\n", "print(np.sqrt(53))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }